package com.kung.qvs.ui.main.fragment;

import android.app.Application;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.google.gson.Gson;
import com.kung.qvs.BR;
import com.kung.qvs.R;
import com.kung.qvs.data.JtRepository;
import com.kung.qvs.entity.BaseResultBean;
import com.kung.qvs.entity.KRecordWeight;
import com.kung.qvs.entity.PrintDataBean;
import com.kung.qvs.entity.ProductBean;
import com.kung.qvs.entity.ProductInfoBean;
import com.kung.qvs.entity.PropInfoBean;
import com.kung.qvs.entity.StatisticsBean;
import com.kung.qvs.entity.WorkerBean;
import com.kung.qvs.entity.database.SqlManager;
import com.kung.qvs.utils.Constants;
import com.kung.qvs.utils.DateUtils;
import com.kung.qvs.utils.SnowFlake;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.binding.command.BindingConsumer;
import me.goldze.mvvmhabit.bus.event.SingleLiveEvent;
import me.goldze.mvvmhabit.http.BaseResponseBean;
import me.goldze.mvvmhabit.http.ResponseThrowable;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.StringUtils;
import me.goldze.mvvmhabit.utils.ToastUtils;
import me.goldze.mvvmhabit.utils.Utils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;
import okhttp3.MediaType;
import okhttp3.RequestBody;

public class MainViewModel extends BaseViewModel<JtRepository> {

    SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //设置日期格式
    SimpleDateFormat kjdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");  //设置日期格式
    SimpleDateFormat kjdf2 = new SimpleDateFormat("yyyy-MM-dd");  //设置日期格式
    private List<ProductInfoBean> garbageTypeBeans = new ArrayList<>();

    public ObservableField<String> mUnit = new ObservableField<>("0");    //重量单位 0： KG,市斤:1：jin

    public ObservableField<String> mProductId = new ObservableField<>();   //产品id
    public ObservableField<String> mProductName = new ObservableField<>(); //产品名称
    public ObservableField<String> mSzWeight = new ObservableField<>("");    //生粽重量
    public ObservableField<ProductBean> mSelectedProductBean = new ObservableField<>();
    public ObservableField<WorkerBean> mWorkerBean = new ObservableField<>(); //选中的工人
    public ObservableField<String> mProductClassId = new ObservableField<>();    //产品分类
    public ObservableField<String> mWeight = new ObservableField<>("0.00"); //重量
    public ObservableField<String> mAmount = new ObservableField<>("0.0"); //数量
    public ObservableField<String> mTime = new ObservableField<>(); //计算总价时间

    //合计
    public ObservableField<String> sTotalWeight = new ObservableField<>("0.0");    //总重量
    public ObservableField<String> sTotalCount = new ObservableField<>("0");    //收集数量

    public ObservableField<String> mOriginPlace = new ObservableField<>("");    //产地
    public ObservableField<String> mProducer = new ObservableField<>("");    //生产者
    public ObservableField<String> mContact = new ObservableField<>("");    //联系方式
    private final static String mDefaultKjDate = "1900-01-01 00:00:00.000";    //默认开具日期
    public ObservableField<String> mKjDateShow = new ObservableField<>("");    //开具日期

    private SimpleDateFormat sdf_show = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  //设置日期格式

//    public ObservableField<Integer> mPrintModel = new ObservableField<>(2);    //1 彩色打印，2空白纸打印
    public ObservableField<Integer> mPrintSelected = new ObservableField<>(1);
    //是否挂牌
    public ObservableField<Integer> mIsHang = new ObservableField<>(0);

    public ObservableField<String> mCompanyTitle = new ObservableField<>("");    //公司名称
    public ObservableField<String> mTareWeight = new ObservableField<>("0.0");    //皮重

    public ObservableField<String> lastWeight = new ObservableField<>("");    //上次称重

    //查询品名关键字
    public ObservableField<String> mSearchKey = new ObservableField<>("");    //品名
    public boolean isUploadAllowed = true;    //是否允许上传

    //封装一个界面发生改变的观察者
    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        //下拉刷新完成
        public SingleLiveEvent<String> eventChangeTrashType = new SingleLiveEvent<>();
        public SingleLiveEvent eventToSettingPage = new SingleLiveEvent();
        public SingleLiveEvent eventToCollectRecordPage = new SingleLiveEvent();
        public SingleLiveEvent eventBleBalance = new SingleLiveEvent();
        public SingleLiveEvent eventBlePrinter = new SingleLiveEvent();

        public SingleLiveEvent eventCommondCumulative = new SingleLiveEvent();  //询问是否保存此记录
        public SingleLiveEvent eventCommondClearAndUpload = new SingleLiveEvent();  //询问是否清零
        public SingleLiveEvent<String> eventChangeQrCode = new SingleLiveEvent<>(); //更新二维码
        public SingleLiveEvent eventCommondQP = new SingleLiveEvent();  //去皮

        public SingleLiveEvent<PrintDataBean> eventPrintCommond = new SingleLiveEvent();
        public SingleLiveEvent eventSelectWorker = new SingleLiveEvent();

        public SingleLiveEvent eventToInputTare = new SingleLiveEvent();
        public SingleLiveEvent eventToInputCompany = new SingleLiveEvent();
        public SingleLiveEvent eventPlaySound = new SingleLiveEvent();
        public SingleLiveEvent eventPlaySoundNoWorker = new SingleLiveEvent();

        public SingleLiveEvent eventToUploadDetail = new SingleLiveEvent();

        public SingleLiveEvent eventSelectProductType = new SingleLiveEvent<>();

        public SingleLiveEvent<Integer> eventChangePrintCount = new SingleLiveEvent<>();
    }

    public MainViewModel(@NonNull Application application, JtRepository model) {
        super(application, model);
    }

    //垃圾分类
    public ObservableList<MainTrashItemViewModel> observableListTrash = new ObservableArrayList<>();
    public ItemBinding<MainTrashItemViewModel> itemBindingTrash = ItemBinding.of(BR.viewModel, R.layout.item_trash_type);


    @Override
    public void onCreate() {
        super.onCreate();
    }

    @Override
    public void onResume() {
        super.onResume();

        //加载产品列表
//        mProductName.set("");
//        mProductId.set("");
//        mSzWeight.set("");
//        mSelectedProductBean.set(null);

//        observableListTrash.clear();
//        List<ProductBean> list = SqlManager.queryProductInfoBeans();
//        for (ProductBean bean : list) {
//            if (model.getProduct().equals(bean.getProductId())) {
//                mProductName.set(bean.getProductName());
//                mProductId.set(bean.getProductId());
//                mSzWeight.set(bean.getWeight());
//                mSelectedProductBean.set(bean);
//            }
//            observableListTrash.add(new MainTrashItemViewModel(MainViewModel.this, bean));
//        }
//        if (mSelectedProductBean.get() != null) {
//            setGarbageTypeSelectState(mSelectedProductBean.get(), false);
//        }
    }

    public void reLoadSomeData() {
        initDate();
        doRefreshStatic();
        mCompanyTitle.set(model.getCompanyName());
        mTareWeight.set(model.getTare());

        //加载产品列表
//        mProductName.set("");
//        mProductId.set("");
//        mSzWeight.set("");
//        mSelectedProductBean.set(null);
//
//        observableListTrash.clear();
//        List<ProductBean> list = SqlManager.queryProductInfoBeans();
//        for (ProductBean bean : list) {
//            if (model.getProduct().equals(bean.getProductId())) {
//                mProductName.set(bean.getProductName());
//                mProductId.set(bean.getProductId());
//                mSzWeight.set(bean.getWeight());
//                mSelectedProductBean.set(bean);
//            }
//            observableListTrash.add(new MainTrashItemViewModel(MainViewModel.this, bean));
//        }
//        if (mSelectedProductBean.get() != null) {
//            setGarbageTypeSelectState(mSelectedProductBean.get(), false);
//        }
    }

    public void reLoadSomeData2() {
        //加载产品列表
        mProductName.set("");
        mProductId.set("");
        mSzWeight.set("");
        mSelectedProductBean.set(null);

        observableListTrash.clear();
        List<ProductBean> list = SqlManager.queryProductInfoBeans();
        for (ProductBean bean : list) {
            if (model.getProduct().equals(bean.getProductId())) {
                mProductName.set(bean.getProductName());
                mProductId.set(bean.getProductId());
                mSzWeight.set(bean.getWeight());
                mSelectedProductBean.set(bean);
            }
            observableListTrash.add(new MainTrashItemViewModel(MainViewModel.this, bean));
        }
        if (mSelectedProductBean.get() != null) {
            setGarbageTypeSelectState(mSelectedProductBean.get(), false);
        }
    }

    public BindingCommand onEditTitle = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToInputCompany.call();
        }
    });

    public BindingCommand onEditTare = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToInputTare.call();
        }
    });

    //更多设置
    public BindingCommand onToSettingPage = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToSettingPage.call();
//            uc.eventCommondQP.call();
        }
    });

    //同步记录
    public BindingCommand onToCollectRecordPage = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToCollectRecordPage.call();
        }
    });

    //蓝牙秤
    public BindingCommand onBleBalanceCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            uc.eventBleBalance.call();    //蓝牙秤
            uc.eventBlePrinter.call();  //蓝牙打印机
        }
    });

    //打印
//    public BindingCommand onPrintCommand = new BindingCommand(new BindingAction() {
//        @Override
//        public void call() {
//            //打印今日最新一条
//            PrintDataBean printDataBeanLatest = SqlManager.queryPrintDataBeanTodayLatest(model.getEquipmentNo(), DateUtils.getCurrentDateTime());
//            if (printDataBeanLatest != null) {
//                doPrintLabel(printDataBeanLatest);
//            } else {
//                ToastUtils.showShort("暂无可打印记录");
//            }
//        }
//    });
    //保存称重
    public BindingCommand onSaveWeightCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventCommondCumulative.call();
        }
    });

    public boolean hasTrashTypes() {
        return observableListTrash.size() > 0;
    }

    public BindingCommand<String> onRadioGroupCheckChanged = new BindingCommand<String>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            if("不打印".equals(s)) {
                mPrintSelected.set(0);
                uc.eventChangePrintCount.setValue(0);
            }else if("打印1份".equals(s)){
//                mPrintModel.set(1);
                mPrintSelected.set(1);
                uc.eventChangePrintCount.setValue(1);
            }else{
//                mPrintModel.set(2);
                mPrintSelected.set(2);
                uc.eventChangePrintCount.setValue(2);
            }
        }
    });

    public BindingCommand<String> onRadioGroupCheckGPChanged = new BindingCommand<String>(new BindingConsumer<String>() {
        @Override
        public void call(String s) {
            if("是".equals(s)) {
                mIsHang.set(1);
            } else{
                mIsHang.set(0);
            }
        }
    });

    public BindingCommand onSelectedWorkerCommand = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventSelectWorker.call();
        }
    });

    public BindingCommand onSearchUploadDetail = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
            uc.eventToUploadDetail.call();
        }
    });

    private void initDate() {
        Calendar day = Calendar.getInstance();
        String times_str = sdf_show.format(day.getTime());
        mKjDateShow.set(times_str);
    }

    //初始化垃圾类型
    public void initProductTypes() {
        model.reqSynchronizeProducts("", model.getEquipmentNo()).compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求数据...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResultBean<List<ProductBean>>>() {
                    @Override
                    public void onNext(BaseResultBean<List<ProductBean>> response) {
                        //请求成功
                        if (response.isSuccess()) {
                            observableListTrash.clear();
                            //数据插入本地，并根据id排序查询。已被无网络情况使用
                            if (response.entity == null || response.entity.size() < 1) {
                                return;
                            }
                            SqlManager.deleteProductBeans();
                            for(ProductBean bean : response.entity) {
                                SqlManager.insertOrReplaceProductInfoBean(bean);
                            }

                            List<ProductBean> list = SqlManager.queryProductInfoBeans();
                            for (ProductBean bean : list) {
                                if (model.getProduct().equals(bean.getProductId())) {
                                    mProductName.set(bean.getProductName());
                                    mProductId.set(bean.getProductId());
                                    mSzWeight.set(bean.getWeight());
                                    mSelectedProductBean.set(bean);
                                }
                                observableListTrash.add(new MainTrashItemViewModel(MainViewModel.this, bean));
                            }

                            if (mSelectedProductBean.get() != null) {
                                setGarbageTypeSelectState(mSelectedProductBean.get(), false);
                            }
                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
//                            ToastUtils.showShort(response.getErrorMsg());
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
//                        dismissDialog();
                        if (throwable instanceof java.util.concurrent.TimeoutException) {

                        } else if (throwable instanceof ResponseThrowable) {
                                ToastUtils.showShort(((ResponseThrowable) throwable).message);
                                //token失效，重新登录
//                            finish();
                            //如果无网络，使用本地缓存垃圾分类
                        }
                    }

                    @Override
                    public void onComplete() {
                        //关闭对话框
//                        dismissDialog();
                    }
                });
    }

    public void funGetProducts(String key) {
        //加载产品列表
        mProductName.set("");
        mProductId.set("");
        mSzWeight.set("");
        mSelectedProductBean.set(null);
        observableListTrash.clear();
        List<ProductBean> list = SqlManager.queryProductInfoBeansByKey(key);
        for (ProductBean bean : list) {
            if (model.getProduct().equals(bean.getProductId())) {
                mProductName.set(bean.getProductName());
                mProductId.set(bean.getProductId());
                mSzWeight.set(bean.getWeight());
                mSelectedProductBean.set(bean);
            }
            observableListTrash.add(new MainTrashItemViewModel(MainViewModel.this, bean));
        }
        if (mSelectedProductBean.get() != null) {
            setGarbageTypeSelectState(mSelectedProductBean.get(), false);
        }
    }

    private void doPrintLabel(PrintDataBean printDataBean){
        uc.eventPrintCommond.setValue(printDataBean);
    }

    //设置选中的垃圾ui
    public void setGarbageTypeSelectState(ProductBean obj, boolean isSelect) {
        if (obj != null) {
            mProductName.set(obj.getProductName());
            mProductId.set(obj.getProductId());
            mSzWeight.set(obj.getWeight());
            mSelectedProductBean.set(obj);
            model.saveProduct(obj.getProductId());
            if (!StringUtils.isTrimEmpty(mWeight.get())) {
                mTime.set(df.format(new Date()));
            }
        } else {
            mProductName.set("");
            mProductId.set("");
            mSzWeight.set("");
            mSelectedProductBean.set(null);
            model.saveProduct("");
        }


        MainTrashItemViewModel tempViewModel;
        for (int i = 0; i < observableListTrash.size(); i++) {
            tempViewModel = observableListTrash.get(i);
            if (obj != null && obj.getProductId().equals(tempViewModel.entity.get().getProductId())) {
                tempViewModel.entity.get().setBgDrawable(Utils.getContext().getResources().getDrawable(R.drawable.shape_trash_type_corner_selected));
                tempViewModel.entity.get().setTextColorName(Utils.getContext().getResources().getColor(R.color.trash_type_selected));
                tempViewModel.entity.get().setTextColorPrice(Utils.getContext().getResources().getColor(R.color.trash_type_price_selected));
                tempViewModel.entity.notifyChange();
            } else {
                tempViewModel.entity.get().setBgDrawable(Utils.getContext().getResources().getDrawable(R.drawable.shape_trash_type_corner));
                tempViewModel.entity.get().setTextColorName(Utils.getContext().getResources().getColor(R.color.trash_type_normal));
                tempViewModel.entity.get().setTextColorPrice(Utils.getContext().getResources().getColor(R.color.trash_type_price_normal));
                tempViewModel.entity.notifyChange();
            }
        }
        if (isSelect) {
            uc.eventSelectProductType.call();
        }
    }

    String lastUserName = "";
    String lastProductId = "";
    Date lastDate = null;

    //保存称重并打印
    public void doSaveTrashRecord() {
        if (StringUtils.isTrimEmpty(mCompanyTitle.get())) {
            ToastUtils.showShort("请输入打印公司名称");
            return;
        }

        if (StringUtils.isTrimEmpty(mProductId.get())) {
            ToastUtils.showShort("请选择产品");
            return;
        }

        if (mWorkerBean == null || mWorkerBean.get() == null) {
            ToastUtils.showShort("请选择工人");
            uc.eventPlaySoundNoWorker.call();
            return;
        }

        if (StringUtils.isTrimEmpty(mWeight.get()) || Double.parseDouble(mWeight.get()) <= 0) {
            ToastUtils.showShort("请称重");
            return;
//            mWeight.set("126.7");
        }

        if (StringUtils.isTrimEmpty(model.getEquipmentNo())) {
            ToastUtils.showShort("没有获取到设备编号");
            return;
        }

        //如果重复称重，且时间在20秒内，不允许保存
        if (!StringUtils.isTrimEmpty(lastUserName) && !StringUtils.isTrimEmpty(lastProductId)) {
            if (lastUserName.equals(mWorkerBean.get().getUserName()) && lastProductId.equals(mProductId.get())) {
                if (lastDate != null) {
                    long diff = new Date().getTime() - lastDate.getTime();
                    long seconds = diff / 1000;
                    if (seconds < 20) {
                        ToastUtils.showShort("请勿重复称重");
                        return;
                    }
                }
            }
        }

        //保存数据
        model.reqUploadWeightRecord("", model.getEquipmentNo(),mProductId.get(), mProductName.get(),
                        mSelectedProductBean.get().getWeight(), mSelectedProductBean.get().getUnit(), mWorkerBean.get().getUserId(),
                        mWorkerBean.get().getUserName(), mWorkerBean.get().getNickName(), mWeight.get(), mAmount.get(),
                        mCompanyTitle.get(), String.valueOf(mIsHang.get()))
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .timeout(Constants.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求数据...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResultBean<KRecordWeight>>() {
                    @Override
                    public void onNext(BaseResultBean<KRecordWeight> response) {
                        //请求成功
                        if (response.isSuccess()) {
                            ToastUtils.showShort(response.desc);
                            //执行打印标签
                            PrintDataBean printDataBean = new PrintDataBean();
                            printDataBean.setEquipmentNo(model.getEquipmentNo());
                            printDataBean.setCompanyName(mCompanyTitle.get());
                            printDataBean.setProductId(mProductId.get());
                            printDataBean.setProductName(mProductName.get());
                            printDataBean.setStandardWeight(mSelectedProductBean.get().getWeight());
                            printDataBean.setWeight(mWeight.get());
                            printDataBean.setAmount(mAmount.get());
                            printDataBean.setWorkId(mWorkerBean.get().getUserName());
                            printDataBean.setWorkName(mWorkerBean.get().getNickName());
                            printDataBean.setFcdate(df.format(new Date()));
                            printDataBean.setPrintCode(response.entity.getRecordId());
                            printDataBean.setListing(mIsHang.get() + "");

                            //记录上次称重
                            lastUserName = mWorkerBean.get().getUserName();
                            lastProductId = mProductId.get();
                            lastDate = new Date();
                            //清掉工人
                            mWorkerBean.set(null);

                            for (int i = 0; i < mPrintSelected.get(); i++) {
                                doPrintLabel(printDataBean);
                            }
                            doRefreshStatic();
                            uc.eventPlaySound.call();

                        } else {
                            //code错误时也可以定义Observable回调到View层去处理
                            ToastUtils.showShort(response.desc);
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
//                        dismissDialog();
                        if (throwable instanceof java.util.concurrent.TimeoutException) {

                        } else if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                            //token失效，重新登录
//                            finish();
                            //如果无网络，使用本地缓存垃圾分类
                            mProductName.set("");
                            mProductId.set("");
                            mSzWeight.set("");
                            mSelectedProductBean.set(null);
                            observableListTrash.clear();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }

    private void doRefreshStatic() {
        //清除列表
        sTotalWeight.set("0.0");
        sTotalCount.set("0");
        model.reqGetWeightRecordStatistics("", model.getEquipmentNo())
                .compose(RxUtils.schedulersTransformer()) //线程调度
                .compose(RxUtils.exceptionTransformer()) // 网络错误的异常转换, 这里可以换成自己的ExceptionHandle
                .doOnSubscribe(this)//请求与ViewModel周期同步
                .timeout(Constants.DEFAULT_TIMEOUT, TimeUnit.SECONDS)
                .doOnSubscribe(new Consumer<Disposable>() {
                    @Override
                    public void accept(Disposable disposable) throws Exception {
//                        showDialog("正在请求数据...");
                    }
                })
                .subscribe(new DisposableObserver<BaseResultBean<StatisticsBean>>() {
                    @Override
                    public void onNext(BaseResultBean<StatisticsBean> response) {
                        //请求成功
                        if (response.isSuccess()) {
                            sTotalCount.set(response.entity.getCount() + "");
                            sTotalWeight.set(response.entity.getWeight() + "");
                        }
                    }

                    @Override
                    public void onError(Throwable throwable) {
                        //关闭对话框
//                        dismissDialog();
                        if (throwable instanceof java.util.concurrent.TimeoutException) {

                        } else if (throwable instanceof ResponseThrowable) {
                            ToastUtils.showShort(((ResponseThrowable) throwable).message);
                            //token失效，重新登录
//                            finish();
                        }
                    }

                    @Override
                    public void onComplete() {
                        //关闭对话框
                        dismissDialog();
                    }
                });
    }

    public static String addZeroForNum(String str, int strLength) {
        int strLen = str.length();
        StringBuffer sb = null;
        while (strLen < strLength) {
            sb = new StringBuffer();
            sb.append("0").append(str);// 左补0
            // sb.append(str).append("0");//右补0
            str = sb.toString();
            strLen = str.length();
        }
        return str;
    }

    public void localSetTare() {
        model.saveTare(mTareWeight.get());
    }

    public void localSetCompanyTitle() {
        model.saveCompanyName(mCompanyTitle.get());
    }

    public void queryLocalProduct(String productId) {
        ProductBean bean = SqlManager.queryProductInfoBeanById(productId);
        if (bean != null) {
            setGarbageTypeSelectState(bean, true);
        } else {
            setGarbageTypeSelectState(null, false);
            ToastUtils.showShort("未找到该产品");
        }
    }
}
